<?php

$params = require __DIR__ . '/params.php';
$db     = require __DIR__ . '/db.php';

$config = [
    'id'         => 'basic',
    'basePath'   => dirname(__DIR__),
    'bootstrap'  => ['log'],
    'aliases'    => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    'components' => [
        'request'  => [
            'cookieValidationKey'    => 'test',
            'class'                  => '\yii\web\Request',
            'enableCookieValidation' => false,
            'parsers'                => [
                'application/json' => 'yii\web\JsonParser',
            ]
        ],
        'response' => [
            'class'         => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                //restful api
                $response = $event->sender;
                $code     = $response->getStatusCode();
                $msg      = $response->statusText;
                if ($code == 404) {
                    !empty($response->data['message']) && $msg = $response->data['message'];
                }
                //设置固定返回数据参数
                $data = [
                    'code' => $code,
                    'msg'  => $msg,
                    'data' => $response->data
                ];
                $code == 200 && $data['data'] = $response->data;
                $response->data   = $data;
                $response->format = yii\web\Response::FORMAT_JSON;
            },
        ],

        'cache'        => [
            'class' => 'yii\caching\FileCache',
        ],
        'user'         => [
            'identityClass'   => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer'       => [
            'class'            => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log'          => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets'    => [
                [
                    'class'  => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db'           => $db,

        'urlManager' => [
            'enablePrettyUrl'     => true,
            'showScriptName'      => false,
            'enableStrictParsing' => true,
            /*'rules'               => [
                [
                    'class'      => 'yii\rest\UrlRule',
                    'controller' => ['api/project-team'],
                    'pluralize'  => false  //不在url链接中的project-team后加s 复数
                ],
                [
                    'controller'    => ['api/user'],
                    'class'         => 'yii\rest\UrlRule',
                    'pluralize'     => false,
                    'extraPatterns' => [
                        'POST login'       => 'login',
                        'GET  reg'         => 'reg',
                        //支持OPTIONS请求
                        'GET,OPTIONS info' => 'info',
                    ],
                ],
            ],*/
            //路由规则配置
            'rules'               => require(__DIR__ . '/url-rules.php')
        ],


    ],
    'params'     => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][]      = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
    $config['modules']['api']   = [
        'class' => 'app\modules\api\Module',
    ];
    $config['bootstrap'][]      = 'gii';
    $config['modules']['gii']   = [
        'class'      => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        'allowedIPs' => ['127.0.0.1', '::1'],
    ];
}

return $config;
